<?php
/** 
 * facility_model
 * This class handles model work necessary for facility Crud and List operations 
 * 
 * @author Ray Nowell
 *	
 */ 
class Facility_model extends MY_Model {

	function Facility_model()
	{
		parent::MY_Model();
	}
	
	function Add_facility($data)
	{
		return $this->db->insert('facility', $data);
	}
	
	function Update_facility($data)
	{
		$this->db->where('facility_id',element('facility_id', $data));
		return $this->db->update('facility',$data);
	}
	
	function Delete_facility()
	{
		$ret_val = $this->delete_record('facility','facility_id',false);
		return $ret_val;
	}
	
	/*
	 * Returns a list of current facilities
	 * @todo must filter by logged in user
	 */
	function get_facility_list()
	{
		$select_fields =	"	 facility_id
								,facility_name
								,CONCAT('<a href=\"',facility_site_url,'\" target=\"_blank\">',facility_site_url,'</a>')
								,CASE active WHEN 1 THEN 'Yes' Else 'No' END as active";
		//Get the query results for the clinical facilities
		$this->db->select($select_fields, false);
		$this->db->from('facility');
		$query = $this->db->get();
		 
		return $query->result_array();
	}

	/*
	 * Returns a single facility record based on facility Id passed 
	 */
	function get_facility_id_record($facility_id)
	{
		 $query = $this->db->limit(1)->get_where('facility', array('facility_id' => $facility_id));

		 return $query->row();
	}
	
	/*
	 * Returns a single facility record based on facility Number passed 
	 */
	function get_facility_number_record($facility_id_number)
	{
		$query = $this->db->limit(1)->get_where('facility', array('facility_id_number' => $facility_id_number));
		 
		if ($query->num_rows() > 0)
			$query->row();
		else
			return false;
	}
	
	//Retuns true/false to see if the user's facility id (current_facility_id) is an active facility
	//Inactive facilities do not allow logins
	function get_active_facility_via_userid($user_id)
	{

	}
}

/* End of file facility_model.php */
/* Location: ./system/application/models/facility_model.php */